package code.ws_common_scene.manager
{
	import code.ws_common_ems.ModeActionEms;
	import code.ws_common_ems.ModeRenderEms;
	import code.ws_common_ems.ModeStatusEms;
	import code.ws_common_scene.GameScene;
	import code.ws_common_scene.data.AtkData;
	import code.ws_common_scene.map.MapData;
	import code.ws_common_scene.map.MapModePool;
	import code.ws_common_scene.map.XMapMode;
	import code.ws_common_scene.mode.Monster;
	import code.ws_common_scene.mode.Trans;
	import code.ws_common_scene.mode.base.Fighter;
	import code.ws_common_utils.IDUtils;
	import code.ws_common_utils.MathUtils;
	import code.ws_common_utils.NameUtils;
	
	import flash.geom.Point;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.utils.Color;

	/**
	 * 场景艺术演员管理器（单例类） 
	 * @author qiangchen
	 * 
	 */	
	public class SceneArticleManager
	{
		private static var _instance:SceneArticleManager = null;
		
		/**
		 * 构造函数 
		 * 
		 */		
		public function SceneArticleManager()
		{
			if( _instance )
			{
				throw new Error("SceneArticleManager--->(Error): 唯一实例对象，不可进行二次创建。");
				return;
			}
			
			_instance = this;
		}
		
		/**
		 * 创建唯一实例对象 
		 * @return 
		 * 
		 */		
		public static function get instance():SceneArticleManager
		{
			if( _instance == null )
			{
				_instance = new SceneArticleManager();
			}
			return _instance;
		}
		
		
		/**
		 * 创建传送阵 
		 * @param mode
		 * @param data
		 */		
		public function cTrans( mode:XMapMode, data:MapData ):void
		{
			for each( var d:Object in mode._transInfos )
			{
				var trans:Trans = new Trans( "chuansongzhen_stand", d.pos, d.nextMapName, d.nextMapId );
				trans.showid = IDUtils.cId();
				trans.x = trans.tiles.x * data.tileW + data.hTileW;
				trans.y = trans.tiles.y * data.tileH + data.hTileH;
				SceneManager.instance.scene._eyeDic[trans.showid] = trans;
				SceneManager.instance.scene._articleCons.addChildAt( trans, 0 );
				trans = null;
			}
		}
		
		
		/**
		 * 创建怪物 
		 * @param mapId 地图id
		 * @param data 地图数据
		 */		
		public function cMonsters( mapid:String, data:MapData ):void
		{
			var xMapMode:XMapMode = MapModePool.getMapMode( mapid );
			if( xMapMode == null )
			{
				mapid = null;
				cp = null;
				data = null;
				return;
			}
			
			var kinds:Array = xMapMode._monsterKinds;
			if( kinds == null || kinds.length == 0 )
			{
				xMapMode = null;
				mapid = null;
				kinds = null;
				data = null;
				cp = null;
				return;
			}
			
			// 布怪中心点
			var cp:Point = new Point();
			if( mapid == "1001" ) 
				cp.setTo( 22, 73 );
			else if( mapid == "1002" )
				cp.setTo( 37, 36 );
			
			// 根据地图id最后一位值计算需创建的怪物
			var num:int = int( mapid.substr( mapid.length-1 ) ) * int( (Math.random() + 1) * 1.5 );
			var prev_x:int, prev_y:int;
			
			// 创建
			for( var i:int=0; i<kinds.length; i++ )
			{
				var style:String = kinds[i];
				for( var j:int=0; j<num; j++ )	
				{
					var monster:Monster = new Monster( ModeRenderEms.MONSTER, style,  ModeActionEms.STAND, int(Math.random() * 8) );
					monster.showid = IDUtils.cId();
					monster.setStatus( ModeStatusEms.STAND );
					monster.setName( NameUtils.name );
					monster.setNameColor( Color.YELLOW );
					monster.setMoveSpeed( 180 );
					monster.power = 70;
					monster.maxHp = 400 + int( Math.random() * 100 );
					monster.hp = monster.maxHp;
					monster.setDirection( monster.direction );
					
					var ti:Point = new Point ( MathUtils.cValInCompare(cp.x, 4, prev_x),  MathUtils.cValInCompare(cp.y, 4, prev_y) );
					monster.tiles =  ti ;
					
					monster.x = ti.x * data.tileW + data.hTileW;
					monster.y = ti.y * data.tileH + data.hTileH;
					SceneManager.instance.scene._eyeDic[monster.showid] = monster;
					SceneManager.instance.scene._articleCons.addChild( monster );
					
					prev_x = ti.x;
					prev_y = ti.y;
					monster = null;
					ti = null;
				}
				style = null;
				data = null;
			}
			
			// 释放
			xMapMode = null;
			mapid = null;
			kinds = null;
			num =  prev_x = prev_y = NaN;
			cp = null;
		}
		
		
		/*---------------------------------------------------------------------------------------------------------------------------*/
		
		
		/**
		 * 战斗对象受伤 
		 * @param data
		 * 
		 */		
		public function hurt( data:AtkData ):void
		{
			if( data == null ) return;
			
//			trace( "create hurt value: "+( data._damage as HpDamage )._count );
			var toDis:Fighter = SceneManager.instance.scene._eyeDic[ data._toid ] as Fighter;
			if( toDis == null )
			{
				data.dispose();
				data = null;
				return;
			}
			
			if( toDis.status == ModeStatusEms.DEAD )
			{
				data.dispose();
				data = null;
				return;
			}
			
			// 受伤
			toDis.hurt( data._damage );
			toDis = null;
			
			// 释放
			data.dispose();
			data = null;
		}
		
		
		/**
		 * 从场景中删除一个舞台表演者 
		 * @param showid 表演者唯一id
		 * 
		 */		
		public function removeArticle( showid:int ):void
		{
			var scene:GameScene = SceneManager.instance.scene;
			var target:DisplayObject = scene._eyeDic[ showid ] as DisplayObject;
			if( target == null )
			{
				showid = NaN;
				scene = null;
				return;
			}
			
			Starling.juggler.removeTweens( target );
			IDUtils.dId( showid );
			
			target.dispose();
			target.parent && target.parent.removeChild( target, true );
			target = null;
			
			scene._eyeDic[ showid ] = null;
			delete scene._eyeDic[ showid ];
			
			// 释放引用
			scene = null;
			showid = NaN;
		}
	}
}